package com.pshcp.doctor.api;

import com.external.common.WebServiceException;
import com.external.service.WsRequestService;
import com.external.ws.soap.entity.dto.DoctorRegListDTO;
import com.external.ws.soap.entity.res.DoctorReg;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.framework.util.DateUtil;
import com.pshcp.doctor.ao.common.CommonAO;
import com.pshcp.doctor.ao.workcalendar.DoctorWorkCalendarMemoListAO;
import com.pshcp.doctor.ao.workcalendar.DoctorWorkCalendarSummaryAO;
import com.pshcp.doctor.ao.workcalendar.SaveDoctorWorkCalendarMemoAO;
import com.pshcp.doctor.tool.DoctorWorkCalendarTool;
import com.pshcp.doctor.vo.workcalendar.DoctorWorkCalendarDataVO;
import com.pshcp.doctor.vo.workcalendar.DoctorWorkCalendarListVO;
import com.pshcp.domain.dto.DoctorWorkCalendarListDTO;
import com.pshcp.domain.dto.PatientReserveRecordDTO;
import com.pshcp.domain.model.Doctor;
import com.pshcp.domain.model.DoctorWorkCalendarMemo;
import com.pshcp.service.DoctorService;
import com.pshcp.service.DoctorWorkCalendarMemoService;
import com.pshcp.service.PatientReserveRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/11/4 01:21
 */
@Api(tags = {"医生端-工作日历接口"})
@Slf4j
@RestController
@RequestMapping("/api/doctor/workCalendar")
public class DoctorWorkCalendarAPI {

    @Autowired
    private DoctorWorkCalendarMemoService doctorWorkCalendarMemoService;

    @Autowired
    private WsRequestService wsRequestService;

    @Autowired
    private DoctorService doctorService;
    @Autowired
    private PatientReserveRecordService patientReserveRecordService;


    @ApiOperation("获取工作日历概括信息")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取工作日历概括信息")
    @PostMapping("getDoctorWorkCalendarSummary")
    public ResultDTO<List<DoctorWorkCalendarDataVO>> getDoctorWorkCalendarSummary(@RequestBody DoctorWorkCalendarSummaryAO doctorWorkCalendarSummaryAO) throws BizException {
        CommonTool.checkObj(doctorWorkCalendarSummaryAO.getSyear(), "90000006");
        CommonTool.checkObj(doctorWorkCalendarSummaryAO.getSmm(), "90000006");
        CommonTool.checkObj(doctorWorkCalendarSummaryAO.getSdd(), "90000006");
        CommonTool.checkObj(doctorWorkCalendarSummaryAO.getEyear(), "90000006");
        CommonTool.checkObj(doctorWorkCalendarSummaryAO.getEmm(), "90000006");
        CommonTool.checkObj(doctorWorkCalendarSummaryAO.getEdd(), "90000006");
        List<String> timePeriodFromTwoTimeList = DateUtil.getTimePeriodFromTwoTimeList(doctorWorkCalendarSummaryAO.getSyear(), doctorWorkCalendarSummaryAO.getSmm(), doctorWorkCalendarSummaryAO.getSdd(),
                doctorWorkCalendarSummaryAO.getEyear(), doctorWorkCalendarSummaryAO.getEmm(), doctorWorkCalendarSummaryAO.getEdd());
        if (CollectionUtils.isEmpty(timePeriodFromTwoTimeList)) {
            return ResultBuilder.success(Collections.emptyList());
        }
        Long userId = ReqComTool.getUserId();
        String startTime = timePeriodFromTwoTimeList.get(0);
        String endTime = timePeriodFromTwoTimeList.get(timePeriodFromTwoTimeList.size() - 1);
        List<DoctorWorkCalendarMemo> memoList = doctorWorkCalendarMemoService.list(userId, startTime, endTime, null);
        Doctor doctor = doctorService.findById(userId);
        DoctorRegListDTO doctorRegListDTO = DoctorWorkCalendarTool.buildDoctorRegListDTO(doctor.getHisDoctorId(), startTime, endTime);
        List<DoctorReg> doctorRegList = null;
        try {
            doctorRegList = wsRequestService.queryDoctorRes(doctorRegListDTO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<DoctorWorkCalendarDataVO> list = DoctorWorkCalendarTool.buildGetDoctorWorkCalendarSummary(timePeriodFromTwoTimeList, memoList, doctorRegList);
        return ResultBuilder.success(list);
    }

    @ApiOperation("获取工作日历列表")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取工作日历列表")
    @PostMapping("getDoctorWorkCalendarList")
    public ResultDTO<List<DoctorWorkCalendarListVO>> getDoctorWorkCalendarList(@RequestBody DoctorWorkCalendarMemoListAO doctorWorkCalendarMemoListAO) throws BizException {
        CommonTool.checkObj(doctorWorkCalendarMemoListAO.getTime(), "90000006");
        Long userId = ReqComTool.getUserId();
        String time = doctorWorkCalendarMemoListAO.getTime();
        List<DoctorWorkCalendarMemo> memoList = doctorWorkCalendarMemoListAO.getType() == null || doctorWorkCalendarMemoListAO.getType() == 0 ?
                doctorWorkCalendarMemoService.list(userId, time, time, 0) : null;
        List<DoctorReg> doctorRegList = null;
        if (doctorWorkCalendarMemoListAO.getType() == null || doctorWorkCalendarMemoListAO.getType() == 1) {
            Doctor doctor = doctorService.findById(userId);
            DoctorRegListDTO doctorRegListDTO = DoctorWorkCalendarTool.buildDoctorRegListDTO(doctor.getHisDoctorId(), time, time);
            try {
                doctorRegList = wsRequestService.queryDoctorRes(doctorRegListDTO);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        List<DoctorWorkCalendarListVO> list = DoctorWorkCalendarTool.buildGetDoctorWorkCalendarList(doctorWorkCalendarMemoListAO.getType(), memoList, doctorRegList);
        return ResultBuilder.success(list);
    }


    @ApiOperation("获取工作日历列表New")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取工作日历列表New")
    @PostMapping("getDoctorWorkCalendarListNew")
    public ResultDTO<List<DoctorWorkCalendarListDTO>> getDoctorWorkCalendarListNew(@RequestBody DoctorWorkCalendarMemoListAO doctorWorkCalendarMemoListAO) throws BizException {
        CommonTool.checkObj(doctorWorkCalendarMemoListAO.getTime(), "90000006");
        Long userId = ReqComTool.getUserId();
        String time = doctorWorkCalendarMemoListAO.getTime();
        // 工作日志信息
        List<DoctorWorkCalendarListDTO> list = new ArrayList<>();
        // 查询备忘
        if(doctorWorkCalendarMemoListAO.getType() == null || doctorWorkCalendarMemoListAO.getType() == 0){
            List<DoctorWorkCalendarMemo> lstData = doctorWorkCalendarMemoService.list(userId, time, time, 0);
            DoctorWorkCalendarListDTO doctorWorkCalendarListDTO = new DoctorWorkCalendarListDTO();
            doctorWorkCalendarListDTO.setType(0);
            doctorWorkCalendarListDTO.setCount(CollectionUtils.isEmpty(lstData) ? 0 : lstData.size());
            doctorWorkCalendarListDTO.setItems(CollectionUtils.isEmpty(lstData) ? null : lstData);
            list.add(doctorWorkCalendarListDTO);
        }
        // 查询治疗预约
        if(doctorWorkCalendarMemoListAO.getType() == null || doctorWorkCalendarMemoListAO.getType() == 1){
            Doctor doctor = doctorService.findById(userId);
            List<PatientReserveRecordDTO> lstData = patientReserveRecordService.queryReserveRecordList(0, doctor, time);
            DoctorWorkCalendarListDTO doctorWorkCalendarListDTO = new DoctorWorkCalendarListDTO();
            doctorWorkCalendarListDTO.setType(1);
            doctorWorkCalendarListDTO.setCount(CollectionUtils.isEmpty(lstData) ? 0 : lstData.size());
            doctorWorkCalendarListDTO.setItems(CollectionUtils.isEmpty(lstData) ? null : lstData);
            list.add(doctorWorkCalendarListDTO);
        }
        // 查询手术预约
        if(doctorWorkCalendarMemoListAO.getType() == null || doctorWorkCalendarMemoListAO.getType() == 2){
            Doctor doctor = doctorService.findById(userId);
            List<PatientReserveRecordDTO> lstData = patientReserveRecordService.queryReserveRecordList(1, doctor, time);
            DoctorWorkCalendarListDTO doctorWorkCalendarListDTO = new DoctorWorkCalendarListDTO();
            doctorWorkCalendarListDTO.setType(2);
            doctorWorkCalendarListDTO.setCount(CollectionUtils.isEmpty(lstData) ? 0 : lstData.size());
            doctorWorkCalendarListDTO.setItems(CollectionUtils.isEmpty(lstData) ? null : lstData);
            list.add(doctorWorkCalendarListDTO);
        }
        return ResultBuilder.success(list);
    }



    @ApiOperation("保存备忘")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "保存备忘")
    @PostMapping("saveDoctorWorkCalendarMemo")
    public ResultDTO<Long> saveDoctorWorkCalendarMemo(@RequestBody SaveDoctorWorkCalendarMemoAO saveDoctorWorkCalendarMemoAO) throws BizException {
        DoctorWorkCalendarTool.checkSaveDoctorWorkCalendarMemo(saveDoctorWorkCalendarMemoAO);
        if (saveDoctorWorkCalendarMemoAO.getId() == null) {
            DoctorWorkCalendarMemo saveDoctorWorkCalendarMemo = DoctorWorkCalendarTool.buildSaveDoctorWorkCalendarMemo(saveDoctorWorkCalendarMemoAO);
            saveDoctorWorkCalendarMemo.setType(0);
            doctorWorkCalendarMemoService.insert(saveDoctorWorkCalendarMemo);
        } else {
            DoctorWorkCalendarMemo doctorWorkCalendarMemo = doctorWorkCalendarMemoService.findById(saveDoctorWorkCalendarMemoAO.getId());
            CommonTool.checkObj(doctorWorkCalendarMemo, "90000007");
            DoctorWorkCalendarMemo updateDoctorWorkCalendarMemo = DoctorWorkCalendarTool.buildSaveDoctorWorkCalendarMemo(doctorWorkCalendarMemo, saveDoctorWorkCalendarMemoAO);
            doctorWorkCalendarMemoService.update(updateDoctorWorkCalendarMemo);

        }
        return ResultBuilder.success(null);
    }

    @ApiOperation("删除备忘")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.LOGIC_DEL, desc = "删除备忘")
    @PostMapping("delDoctorWorkCalendarMemo")
    public ResultDTO<Long> saveDoctorWorkCalendarMemo(@RequestBody CommonAO commonAO) throws BizException {
        CommonTool.checkObj(commonAO.getId(), "90000006");
        DoctorWorkCalendarMemo doctorWorkCalendarMemo = doctorWorkCalendarMemoService.findById(commonAO.getId());
        CommonTool.checkObj(doctorWorkCalendarMemo, "90000007");
        Long userId = ReqComTool.getUserId();
        if (!userId.equals(doctorWorkCalendarMemo.getDoctorId())) {
            return ResultBuilder.failure("90000013");
        }
        doctorWorkCalendarMemoService.delById(commonAO.getId());
        return ResultBuilder.success(null);
    }

}
